import { Instrusion } from "@/Battle/Intrusion.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { BattleEventType } from "@/Battle/Event/BattleEvent.js";
import { BehaviorEventType } from "@/Battle/Event/BehaviorEvent.js";
import { CombatEventPool } from "@/Battle/Event/CombatEventPool.js";
export class ActionApply extends Instrusion{
    constructor(){
        super();
    }

    load(){
        CombatEventBus.on(
            BattleEventType.Action.Update,
            this.onActionUpdate
        )
    }

    unload(){
        CombatEventBus.off(
            BattleEventType.Action.Update,
            this.onActionUpdate
        )
    }

    //行为调用、伤害计算
    onActionUpdate=(event)=>{
        let {source,target,item,action}=event.payload;
        // this.source=event.payload.source;
        // this.target=event.payload.target;
        // this.item=event.payload.item;
        // this.action=event.payload.action;

        let result=target.result;
        action.apply(target);

        let eventType=null;
        if(result.missed){
            eventType=BehaviorEventType.Miss;
        }else if(result.evaded){
            eventType=BehaviorEventType.Evaded;
        }else{
            eventType=BehaviorEventType.Damage.Request;
        }

        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(
                eventType,
                this,
                {
                    source,
                    target,
                    item,
                    action,
                    result
                }
            )
        );
    }

}